﻿/* 2014/8/2 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace Cosmos.Windows.Forms.FileList.DynamicColumn
{
    public class RegexFileNameAttributeExtractor : IDynamicAttributeExtractor
    {
        private const string _columnNameSymbol = "File_Name_Attribute_";
        private const string _replacementPattern = "${{{0}}}";

        private DynamicColumnDefinition[] _columnDefinitions;
        private Regex _fileNameRegex;

        /// <summary>
        /// Initializes an instance of RegexFileNameAttributeExtractor
        /// by specified pattern.
        /// </summary>
        /// <param name="pattern"></param>
        /// <exception cref="ArgumentException">Specified pattern is not available.</exception>
        /// <exception cref="ArgumentNullException">Pattern is null.</exception>"
        public RegexFileNameAttributeExtractor(string pattern, DynamicColumnDefinition[] columns)
        {
            _fileNameRegex = new Regex(pattern);
            _columnDefinitions = columns;
        }

        public void AddColumns(ListView listView)
        {
            if (listView == null)
                return;

            ClearColumns(listView);

            foreach (DynamicColumnDefinition aColumn in _columnDefinitions)
            {
                listView.Columns.Add(String.Concat(_columnNameSymbol, aColumn.Name),
                    aColumn.Text, aColumn.Width);
            }
        }

        public void ClearColumns(ListView listView)
        {
            if (listView == null)
                return;

            List<string> removeColumnNames = new List<string>(listView.Columns.Count);
            foreach (ColumnHeader aColumn in listView.Columns)
            {
                if (aColumn.Name.Contains(_columnNameSymbol))
                    removeColumnNames.Add(aColumn.Name);
            }
            foreach (string aName in removeColumnNames)
            {
                listView.Columns.RemoveByKey(aName);
            }
        }

        public void AddSubItems(ListViewItem fileItem, string fileName)
        {
            if (fileItem == null)
                return;

            int attributeCount = _columnDefinitions.Length;
            for (int i = 0; i < attributeCount; i++)
            {
                ListViewItem.ListViewSubItem subItem = new ListViewItem.ListViewSubItem();

                subItem.Name = String.Concat(_columnNameSymbol,
                    _columnDefinitions[i].Name);

                Match match = _fileNameRegex.Match(fileName);
                if (match.Success)
                {
                    /*string replacement = String.Format(_replacementPattern, _columnDefinitions[i].Name);                    
                    subItem.Text = _fileNameRegex.Replace(fileName, replacement);*/

                    subItem.Text = match.Groups[_columnDefinitions[i].Name].Value;
                }
                else
                {
                    subItem.Text = String.Empty;
                }

                fileItem.SubItems.Add(subItem);
            }
        }
    }
}
